package org.aom.service.menu.impl;

import java.util.List;

import org.aom.dao.menu.ModuleItemDao;
import org.aom.entity.menu.Module;
import org.aom.entity.menu.ModuleItem;
import org.aom.service.base.EntityManager;
import org.aom.service.menu.IModuleItemManager;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value="moduleItemManager")
@Transactional
public class ModuleItemManager extends EntityManager<ModuleItem, Long> implements IModuleItemManager{
	@Autowired
	private ModuleItemDao moduleItemDao;

	@Override
	protected ModuleItemDao getEntityDao() {
		return moduleItemDao;
	}


	@SuppressWarnings("unchecked")
	public List<ModuleItem> pageQueryByModule(int startNum, int limitNum,
			String idColum, Boolean direction, Module module) {
		String order = direction ? "ASC" : "DESC";
		String hql = "from ModuleItem mi where mi.module.id = "
				+ module.getId() + " order by " + idColum + " " + order;
		Query query = getEntityDao().createQuery(hql);
		List<ModuleItem> list = (List<ModuleItem>) query.setFirstResult(
				startNum).setMaxResults(limitNum).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public Integer totlaNumByModule(Module module) {
		String countQueryString = "select count(*) from ModuleItem  mi where mi.module.id = "
				+ module.getId();
		List countList = getEntityDao().find(countQueryString);
		Long number = 0L;
		try {
			number = (Long) countList.get(0);
		} catch (Exception e) {
			return 0;
		}
		return new Integer(number.intValue());
	}
}
